package com.android.job.ui;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.job.fusion.FusionCode;
import com.android.job.fusion.FusionField;
import com.android.job.logic.InterviewLogic;
import com.android.job.model.Resume;

public class ResumeDetailActivity extends BaseActivity implements OnClickListener
{

	private TextView real_name_tv_id;
	private TextView gender_tv_id;
	private TextView degree_detail_tv_id;
	private TextView identity_tv_id;
	private TextView politics_tv_id;
	private TextView mobile_number_tv_id;
	private TextView region_detail_tv_id;
	private TextView  salary_detail_tv_id;
	private TextView  nature_tv_id;
	private TextView type_tv_id;
	private TextView recent_type_tv_id;
	private TextView  recent_year_tv_id;
	private TextView  edu_tv_id;
	private TextView  edu_cate_tv_id;
	private TextView  work_exp_tv_id;
	private TextView  interview; 
	private ImageView back;
	private Intent intent;
	private Resume resume;
	private ProgressDialog pd;
	
	
	private RelativeLayout resume_search;
	private RelativeLayout recent_type;
	private RelativeLayout recent_year;
	private RelativeLayout edu;
	private RelativeLayout edu_cate;
	private RelativeLayout work_exp;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.resume_detail);
		getIntents();
		initView();
	}
	
	
	private void initView(){
		real_name_tv_id = (TextView) findViewById(R.id.real_name_tv_id);
		gender_tv_id = (TextView) findViewById(R.id.gender_tv_id);
		degree_detail_tv_id = (TextView) findViewById(R.id.degree_detail_tv_id);
		identity_tv_id = (TextView) findViewById(R.id.identity_tv_id);
		politics_tv_id = (TextView) findViewById(R.id.politics_tv_id);
	   mobile_number_tv_id = (TextView) findViewById(R.id.mobile_number_tv_id);
	   region_detail_tv_id = (TextView) findViewById(R.id.region_detail_tv_id);
	   salary_detail_tv_id = (TextView) findViewById(R.id.salary_detail_tv_id);
	   nature_tv_id = (TextView) findViewById(R.id.nature_tv_id);
	   type_tv_id = (TextView) findViewById(R.id.type_tv_id);
	   recent_type_tv_id = (TextView) findViewById(R.id.recent_type_tv_id);
	   recent_year_tv_id = (TextView) findViewById(R.id.recent_year_tv_id);
	   edu_tv_id = (TextView) findViewById(R.id.edu_tv_id);
	   edu_cate_tv_id = (TextView) findViewById(R.id.edu_cate_tv_id);
	   work_exp_tv_id = (TextView) findViewById(R.id.work_exp_tv_id);
	   recent_type = (RelativeLayout) findViewById(R.id.recent_type);  
	   recent_year = (RelativeLayout) findViewById(R.id.recent_year);  
	   edu = (RelativeLayout) findViewById(R.id.edu);  
	   edu_cate = (RelativeLayout) findViewById(R.id.edu_cate);  
	   work_exp = (RelativeLayout) findViewById(R.id.work_exp);  
	   resume_search = (RelativeLayout) findViewById(R.id.resume_search);  
      
	
		 

	   
	   
	   interview = (TextView) findViewById(R.id.interview);
    	   interview.setOnClickListener(this);
	   back = (ImageView) findViewById(R.id.search_back);
		back.setOnClickListener(this);
		if (resume != null)
		{
			real_name_tv_id.setText(resume.getRealName());
			 gender_tv_id.setText(resume.getGender());
			degree_detail_tv_id.setText(resume.getDegree());
			identity_tv_id.setText(resume.getIdentity());
			politics_tv_id.setText(resume.getPolitics());
			mobile_number_tv_id.setText(resume.getMobileNumber());
			region_detail_tv_id.setText(resume.getRegion());
			salary_detail_tv_id.setText(resume.getSalary());
			nature_tv_id.setText(resume.getNature());
			type_tv_id.setText(resume.getType());
			
			   recent_type_tv_id.setText(resume.getRecentType());
			   recent_year_tv_id.setText(resume.getRecentYear());
			   edu_tv_id.setText(resume.getEduStr());
			   edu_cate_tv_id.setText(resume.getEduCateStr());
			   work_exp_tv_id.setText(resume.getWorkExpStr());
			   
			   if(("".equals(resume.getWorkExpStr().trim()))){
				   work_exp.setVisibility(View.GONE);
			   }
			   
			   if(("".equals(resume.getEduCateStr().trim()))){
				   edu_cate.setVisibility(View.GONE);
			   }
			   
			   if(("".equals(resume.getEduStr().trim()))){
				   edu.setVisibility(View.GONE);
			   }
			   
			   if(("".equals(resume.getRecentYear().trim()))){
				   recent_year.setVisibility(View.GONE);
			   }
			   Log.e("free.lang","=work_exp="+work_exp.VISIBLE);
			   RelativeLayout[] temp = {work_exp,edu_cate,edu,recent_year,recent_type};
			   for(int i = 0;i < 5;i++){
				   if(temp[i].getVisibility() != View.GONE){
					   temp[i].setBackgroundResource(R.drawable.et_bg3);
					   break;
				   }
			   }
			   
			   
			   
			   
			   
			   
			   
			   
//			   if(("".equals(resume.getWorkExpStr().trim()))&&(!"".equals(resume.getEduCateStr().trim()))){
//				   edu_cate.setBackgroundResource(R.drawable.et_bg3);
//			   }
//			   
//			   if(("".equals(resume.getWorkExpStr().trim()))&&("".equals(resume.getEduCateStr().trim()))&&(!"".equals(resume.getEduStr().trim()))){
//				   edu.setBackgroundResource(R.drawable.et_bg3);
//			   }
//			   
//			   if(("".equals(resume.getWorkExpStr().trim()))&&("".equals(resume.getEduCateStr().trim()))&&("".equals(resume.getEduStr().trim()))&&(!"".equals(resume.getRecentYear().trim()))){
//				   recent_year.setBackgroundResource(R.drawable.et_bg3);
//			   }
//			   if(("".equals(resume.getWorkExpStr().trim()))&&("".equals(resume.getEduCateStr().trim()))&&("".equals(resume.getEduStr().trim()))&&("".equals(resume.getRecentYear().trim()))){
//				   recent_type.setBackgroundResource(R.drawable.et_bg3);
//			   }
			   
		}
	}
	
	
	private void getIntents()
	{
		intent = getIntent();
		if (intent != null)
		{
			Bundle bundle = intent.getExtras();
			if (bundle != null)
			{
				resume = (Resume) bundle.getSerializable("resumedetail");
			}
		}
	}
	
	public void onClick(View v)
	{
		switch (v.getId())
		{
		case R.id.search_back:
			finish();
			break;
		case R.id.interview:
			jumpToSelect(FusionCode.JOB_TYPE_FROM_SHARE_ITEM, 0);
			break;
		default:
			break;
		}
	}
	
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data)
	{

		if (requestCode == 0 && resultCode == 0)
		{
			pd = new ProgressDialog(ResumeDetailActivity.this);
			pd.setCancelable(true);
			pd.setMessage("正在邀请...");
			pd.show();
			
			String choose = data.getStringExtra("chose");
			String userId = "";
			sharedPreferences = getSharedPreferences(FusionField.POSTARR,
					Activity.MODE_PRIVATE);
			if(sharedPreferences!=null)
			{
				userId = sharedPreferences.getString("id", "");
			}
			InterviewLogic.getInstance().interview(handler, userId, resume.getId(), choose);
		}
		super.onActivityResult(requestCode, resultCode, data);
	}
	
	
	private Handler handler = new Handler()
	{

		@SuppressWarnings("unchecked")
		public void handleMessage(Message msg)
		{
			pd.dismiss();
			switch (msg.what)
			{
				case FusionCode.NETWORK_ERROR:
					
					Toast.makeText(ResumeDetailActivity.this, R.string.network_error,
							Toast.LENGTH_SHORT).show();
					break;
				case FusionCode.NETWORK_TIMEOUT_ERROR:
					Toast.makeText(ResumeDetailActivity.this, R.string.network_timeout,
							Toast.LENGTH_SHORT).show();
					break;
				case FusionCode.RETURN_JSONOBJECT:
					String  success =  (String) msg.obj;
					if("1".equals(success)){
						Toast.makeText(ResumeDetailActivity.this, "邀请面试成功",
								Toast.LENGTH_SHORT).show();
					}else if("0".equals(success)){
						Toast.makeText(ResumeDetailActivity.this, "邀请面试失败",
								Toast.LENGTH_SHORT).show();
					}
					break;
				case FusionCode.SERVER_ERROR:
					pd.dismiss();
					String errorInif = (String) msg.obj;
					
					Toast.makeText(ResumeDetailActivity.this,
							errorInif, Toast.LENGTH_SHORT)
							.show();
					break;
			}

		};
	};
	
	
	
	
	

}
